from PySide import QtGui, QtCore
import sys

class PowerWidget(QtGui.QWidget):
	def __init__(self, mainApp, parent = None):
		QtGui.QWidget.__init__(self, parent)
		
		self.mainApp = mainApp
		self.emcStat = self.mainApp.getEmcStat()
		
		self.estopBtn = QtGui.QPushButton(QtGui.QIcon("/home/andris/aScreen/resources/estop.png"), "eStop", self)
		self.estopBtn.setCheckable(True)
		self.estopBtn.clicked.connect(self.estop)
	
		self.powerBtn = QtGui.QPushButton(QtGui.QIcon("/home/andris/aScreen/resources/power1.svg"), "Power", self)
		self.powerBtn.setCheckable(True)
		self.powerBtn.clicked.connect(self.power)
		
		self.box = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight)
		self.box.addWidget(self.estopBtn)
		self.box.addWidget(self.powerBtn)
		self.setLayout(self.box)
		
	def estop(self):
		self.mainApp.setestop()
		
	def power(self):
		self.mainApp.setPower()
		
	def orientationChanged(self):
		if self.mainApp.toolbar.orientation() == QtCore.Qt.Orientation.Vertical:
			self.box.setDirection(QtGui.QBoxLayout.TopToBottom)
		elif self.mainApp.toolbar.orientation() == QtCore.Qt.Orientation.Horizontal:
			self.box.setDirection(QtGui.QBoxLayout.LeftToRight)
		
					
	def updateData(self):
		if self.emcStat.estop == 1:
			self.estopBtn.setChecked(True)
			self.powerBtn.setEnabled(False)
		elif self.emcStat.estop == 0:
			self.estopBtn.setChecked(False)
			self.powerBtn.setEnabled(True)
		
		self.powerBtn.setChecked(self.emcStat.enabled)
